package com.ruoyi.web.controller.biz;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.BsQiyeConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.valid.ValidInsertGroup;
import com.ruoyi.common.valid.ValidUpdateGroup;
import com.ruoyi.system.domain.BsQiye;
import com.ruoyi.system.domain.ro.BsQiyeEnterRo;
import com.ruoyi.system.domain.ro.BsQiyeRo;
import com.ruoyi.system.service.IBsQiyeBusinessDataService;
import com.ruoyi.system.service.IBsQiyeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Pattern;
import java.util.Collections;
import java.util.List;

/**
 * 涉旅企业管理
 *
 * @author ruoyi
 * @date 2023-10-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@Api(tags = "商户端-店铺入驻&我的店铺")
@RequestMapping("/bs/qiyeEnter")
public class BsQiyeEnterController extends BaseController {

    private final IBsQiyeService iBsQiyeService;
    private final IBsQiyeBusinessDataService iBsQiyeBusinessDataService;


    @ApiOperation(value = "商户端-店铺入驻", tags = "商户端-店铺入驻&我的店铺")
    @Log(title = "商户端-店铺入驻", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/enter")
    public Result<?> add(@Validated(ValidInsertGroup.class) @RequestBody BsQiyeEnterRo ro) {
        return Result.toResult(iBsQiyeService.insertByBo(ro));
    }

    @ApiOperation(value = "商户端-我的店铺-删除", tags = "商户端-店铺入驻&我的店铺")
    @ApiImplicitParam(name = "id", value = "企业id")
    @Log(title = "涉旅企业管理", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @GetMapping("/detele")
    public Result<?> remove(@RequestParam Long id) {
        return Result.toResult(iBsQiyeService.deleteWithValidByIds(Collections.singletonList(id), true));
    }

    @ApiOperation(value = "商户端-我的店铺-修改", tags = "商户端-店铺入驻&我的店铺")
    @Log(title = "涉旅企业管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public Result<?> edit(@Validated(ValidUpdateGroup.class) @RequestBody BsQiyeEnterRo ro) {
        BsQiyeRo build = BsQiyeRo.builder().build();
        BeanUtil.copyProperties(ro, build);
        return Result.toResult(iBsQiyeService.update(build));
    }

    @ApiOperation(value = "商户端-我的店铺-列表(包含未数据上报数-统计字段)", tags = {"商户端-数据上报", "商户端-店铺入驻&我的店铺"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "auditStatus", value = "审核状态 0 已认证/待审核 1审核通过 2审核拒绝 详见字典merchant_type"),
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    @GetMapping("/list")
    public Result<TableDataInfo<BsQiye>> enterList(@Pattern(regexp = "^(0)$|^(1)$|^(2)$", message = "auditStatus不合法") @RequestParam String auditStatus) {
        startPage();
        List<BsQiye> list = iBsQiyeService.queryList(BsQiyeRo.builder()
                .auditStatus(auditStatus)
                .createById(SecurityUtils.getUserId())
                .build(), false);
        if (auditStatus.equals(BsQiyeConstants.AUDIT_STATUS.AUDIT_ALLOW)) {
            iBsQiyeBusinessDataService.setUnReportNum(list);
        }
        return Result.success(getDataTable(list));
    }
}
